package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.repository.TravelCommentRepository;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService{

    @Autowired
    private TravelCommentRepository repository;
    @Autowired
    private MongoTemplate template;


    @Override
    public Page query(TravelCommentQuery qo) {
        //1.创建查询对象
        Query query = new Query();
        //2.封装条件查询对象
        Criteria criteria = Criteria.where("travelId").is(qo.getTravelId());
        query.addCriteria(criteria);
        //3.查询总条数
        long count = template.count(query, TravelComment.class);
        //long count = repository.count(query, TravelComment.class);------>这个方法不行
        //4.封装分页查询对象
        //Pageable--->拿它的实现类
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(),
                Sort.Direction.DESC,
                "createTime");
        query.with(pageRequest);
        //5.查询list
        List<TravelComment> list = template.find(query, TravelComment.class);
        //返回Page实现类
        //PageImpl(List<T> content, Pageable pageable, long total)
        return new PageImpl(list,pageRequest,count);
    }

    @Override
    public TravelComment commentAdd(TravelComment comment) {
        //判断id是否存在
        if(!StringUtils.hasLength(comment.getId())){
            comment.setCreateTime(new Date());
        }
        //判断引用评论
        if(comment.getType()==TravelComment.TRAVLE_COMMENT_TYPE){
            if(StringUtils.hasLength(comment.getRefComment().getId())){
                comment.setRefComment(repository.findById(comment.getRefComment().getId()).get());
            }
        }
        repository.save(comment);
        return comment;
    }

    @Override
    public Long count(Long travelId) {
        //2.查询总条数
        Query query = new Query();
        //2.封装条件查询对象
        Criteria criteria = Criteria.where("travelId").is(travelId);
        query.addCriteria(criteria);
        long count = template.count(query, TravelComment.class);
        return count;
    }

    @Override
    public List<TravelComment> getNewTop10() {
        Query query = new Query();
        PageRequest pageRequest = PageRequest.of(0, 9, Sort.Direction.DESC,"createTime");
        query.with(pageRequest);
        List<TravelComment> list = template.find(query, TravelComment.class);
        return list;
    }


}
